package net.muchine.core.dao.user;

import java.util.List;

import org.springframework.stereotype.Component;

import net.muchine.core.dao.GenericDaoImpl;
import net.muchine.core.domain.user.GroupUser;
import net.muchine.core.domain.user.User;
import net.muchine.core.domain.user.UserSearch;

@Component
public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {
	public User findById(String id) {
		return this.sqlSession.selectOne(this.sqlNamespace + ".findById", id);
	}	
	
	public List<User> findByName(String name) {
		return this.sqlSession.selectList(this.sqlNamespace + ".findByName", name);
	}

	public List<User> findByParameters(UserSearch search) {
		if (search != null) {
			String name = search.getName();
			String email = search.getEmail();
			
			if (name != null && !"".equals(name)) search.setName('%' + name + '%');
			if (email != null && !"".equals(email)) search.setEmail('%' + email + '%');
		}
		
		return this.sqlSession.selectList(this.sqlNamespace + ".findByParameters", search);
	}

	public List<User> selectAll() {
		return this.sqlSession.selectList(this.sqlNamespace + ".selectAll");
	}
	
	public List<User> selectUnassignedUsersToGroup(Long seq) {
		return this.sqlSession.selectList(this.sqlNamespace + ".selectUnassignedUsersToGroup");
	}

	public List<User> selectAssignedUsersToGroup(Long seq) {
		return this.sqlSession.selectList(this.sqlNamespace + ".selectAssignedUsersToGroup");
	}
}
